# 206cmake_string/CMakeLists.txt
cmake_minimum_required(VERSION 3.22)
project(cmake_string)

#取出 begin 和 end 之间的内容 test cmake string
set(STR1 "  begin test cmake string end  ")
# 查找的开头字符串
set(BSTR "begin")
# 在 ${STR1} 中查找 begin 的位置存入 b
# cmake位置以0开始
string(FIND ${STR1} ${BSTR} b)
message("FIND ${BSTR} POS ${b}")

string(FIND ${STR1} "end" e)
message("FIND end POS ${e}")

# 去掉 begin 字符位置
# 获取字符串长度
string(LENGTH ${BSTR} size)
math(EXPR b "${b} + ${size}")
message("b = ${b}")

# length 等于开始到结束的长度
math(EXPR length "${e} - ${b}")
message("length = ${length}")

# 获取字串 在${STR1}字符串${b}位置取${length}长度字符串写入substr
string(SUBSTRING ${STR1} ${b} ${length} substr)
message("substr = [${substr}]")

# 去掉头尾 空格 \t \n \r
string(STRIP ${substr} substr)
message("substr = [${substr}]")

# 转成大写
string(TOUPPER ${substr} substr)
message("substr = [${substr}]")

#[[
string JSON
    string(JSON <out-var> [ERROR_VARIABLE <error-var>]
        {GET | TYPE | LENGTH | REMOVE}
        <json-string> <member|index> [<member|index> ...])
    string(JSON <out-var> [ERROR_VARIABLE <error-var>]
        MEMBER <json-string>
        [<member|index> ...] <index>)
    string(JSON <out-var> [ERROR_VARIABLE <error-var>]
        SET <json-string>
        <member|index> [<member|index> ...] <value>)
    string(JSON <out-var> [ERROR_VARIABLE <error-var>]
        EQUAL <json-string1> <json-string2>)
]]

# 测试用 json
# json 对象 {}  json 数组 []
# 格式 key: value
set(tjson
[=[
{
    "webs": {
        "web": [
        {
            "name": "cmake",
            "url": "cmake.org.cn"
        },
        {
            "name": "ffmpeg",
            "url": "ffmpeg.club"
        },
        {
            "name": "tt",
            "url": "tt.club"
        }
        ]
    }
}
]=]
)
message(${tjson})
#[[
string(JSON <out-var> [ERROR_VARIABLE <error-var>]
        GET <json-string> <member|index> [<member|index> ...])
]]
string(JSON var ERROR_VARIABLE evar
    GET ${tjson} webs web 0 name
)
message("webs web 0 name = ${var}")
string(JSON var ERROR_VARIABLE evar
    GET ${tjson} webs web 1 url
)
message("webs web 1 url = ${var}")
message("evar = ${evar}")

string(JSON var ERROR_VARIABLE evar
    GET ${tjson} webs web2 1 url
)
if(${var})
message("webs web2 = ${var}")
else()
message("NOTFOUND")
endif()
message("evar = ${evar}")

# 读取 json 数组长度
#[[
string(JSON <out-var> [ERROR_VARIABLE <error-var>]
        LENGTH <json-string> <member|index> [<member|index> ...])
]]
string(JSON web_count ERROR_VARIABLE evar
    LENGTH  ${tjson} webs web
)
message("JSON LENGTH = ${web_count}")

# json 的增加和修改
#[[
string(JSON <out-var> [ERROR_VARIABLE <error-var>]
        SET <json-string> <member|index> [<member|index> ...])
]]
# json 的增加
string(JSON set_out SET ${tjson} webs web ${web_count} [=[
{
    "name": "cpp",
    "url": "cppds.com"
}
]=])
message("set_out = ${set_out}")
# json 的修改 SET
string(JSON set_out2 SET ${set_out} webs web 1 [=[
{
    "name": "ffmpeg2",
    "url": "ffmpeg.club"
}
]=])
message("set_out2 = ${set_out2}")

# json 的删除 REMOVE
#[[
string(JSON <out-var> [ERROR_VARIABLE <error-var>]
        REMOVE <json-string> <member|index> [<member|index> ...])
]]
string(JSON remove_out REMOVE ${set_out2} webs web 0)
string(JSON remove_out REMOVE ${remove_out} webs web 1)
message("remove_out = ${remove_out}")